package com.xinhe.web.service;

import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.QueryResults;
import com.querydsl.core.types.Projections;
import com.querydsl.jpa.impl.JPAQueryFactory;
import com.xinhe.web.common.Result;
import com.xinhe.web.entity.QSellerShopTypeGoodsRelation;
import com.xinhe.web.entity.SellerShopTypeGoodsRelation;
import com.xinhe.web.reposity.SellerShopTypeGoodsRelationRepository;
import com.xinhe.web.util.Snowflake;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import javax.inject.Provider;
import javax.persistence.EntityManager;

@Service
public class SellerShopTypeGoodsRelationService {
    @Autowired
    private SellerShopTypeGoodsRelationRepository sellerShopTypeGoodsRelationRepository;
    //查询工厂
    private JPAQueryFactory queryFactory;
    @Autowired
    private Provider<EntityManager> entityManager;
    @PostConstruct
    public void init() {
        queryFactory = new JPAQueryFactory(entityManager);
    }

    public QueryResults<SellerShopTypeGoodsRelation> pageSellerShopTypeGoodsRelation(int pageNum, int pageSize) {
        QSellerShopTypeGoodsRelation qSellerShopTypeGoodsRelation = QSellerShopTypeGoodsRelation.sellerShopTypeGoodsRelation;
        BooleanBuilder builder = new BooleanBuilder();
        //OrderSpecifier<Date> sort = qMemberCoupon.createTime.desc();
        //builder.and(qMemberCoupon.removed.eq(0));
        return queryFactory.select(
                Projections.bean(
                        SellerShopTypeGoodsRelation.class,
                        qSellerShopTypeGoodsRelation.goodsId,
                        qSellerShopTypeGoodsRelation.sellerShopTypeId,
                        qSellerShopTypeGoodsRelation.id
                )
        ).from(qSellerShopTypeGoodsRelation)
                .where(builder).offset(pageNum * pageSize).limit(pageSize).fetchResults();
    }

    @Transactional
    public Result saveSellerShopTypeGoodsRelation(Long id,Long sellerShopTypeId,Long goodsId,String remark) {
        SellerShopTypeGoodsRelation relation = this.sellerShopTypeGoodsRelationRepository.findByTypeIdAndGoodsId(sellerShopTypeId,goodsId);
        if (relation != null) {
            return Result.failed(101,"当前商品已添加该分类");
        }
        if (id != null) {
            relation = (SellerShopTypeGoodsRelation)this.sellerShopTypeGoodsRelationRepository.findById(id).get();
            relation.setGoodsId(goodsId);
            relation.setSellerShopTypeId(sellerShopTypeId);
            sellerShopTypeGoodsRelationRepository.saveAndFlush(relation);
        } else {
            relation = new SellerShopTypeGoodsRelation();
            relation.setId(Snowflake.generateId());
            relation.setGoodsId(goodsId);
            relation.setSellerShopTypeId(sellerShopTypeId);
            sellerShopTypeGoodsRelationRepository.save(relation);
        }
        return Result.ok();
    }

    public Result delete(Long sellerShopTypeGoodsRelationId) {
        sellerShopTypeGoodsRelationRepository.deleteById(sellerShopTypeGoodsRelationId);
        return Result.ok();
    }

    public Result findById(Long sellerShopTypeGoodsRelationId) {
        return Result.ok(sellerShopTypeGoodsRelationRepository.findById(sellerShopTypeGoodsRelationId).get());
    }
}
